package com.ywfStudy.ywfliving.commodity.service.impl;

import com.ywfStudy.ywfliving.commodity.dao.SpuInfoDao;
import com.ywfStudy.ywfliving.commodity.entity.SpuInfoEntity;
import com.ywfStudy.ywfliving.commodity.vo.SearchResult;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.awt.event.ItemEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ywfStudy.common.utils.PageUtils;
import com.ywfStudy.common.utils.Query;

import com.ywfStudy.ywfliving.commodity.dao.SkuInfoDao;
import com.ywfStudy.ywfliving.commodity.entity.SkuInfoEntity;
import com.ywfStudy.ywfliving.commodity.service.SkuInfoService;

import javax.annotation.Resource;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuInfo(SkuInfoEntity skuInfoEntity) {
        this.baseMapper.insert(skuInfoEntity);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        QueryWrapper<SkuInfoEntity> wrapper = new QueryWrapper<>();

        //检索条件key
        String key = (String) params.get("key");
        if (StringUtils.isNotBlank(key)){
            wrapper.and(w->{
                w.eq("sku_id", key).or().like("sku_name", key);
            });
        }
        //检索分类
        String catalogId = (String) params.get("catalogId");
        if (StringUtils.isNotBlank(catalogId) && !"0".equalsIgnoreCase(catalogId)){
            wrapper.eq("catalog_id", catalogId);
        }

        //检索条件 品牌
        String brandId = (String) params.get("brandId");
        if (StringUtils.isNotBlank(brandId) && !"0".equalsIgnoreCase(brandId)){
            wrapper.eq("brand_id", brandId);
        }

        //检索条件-价格范围
        String min = (String) params.get("min");
        String max = (String) params.get("max");
        if (StringUtils.isNotBlank(min)){
            wrapper.ge("price", min);
        }
        if (StringUtils.isNotBlank(max)){
            wrapper.le("price", max);
        }
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }

    //装配SpuInfoDao
    @Resource
    private SpuInfoDao spuInfoDao;

    //返回购买用户检索的结果
    @Override
    public SearchResult querySearchPageByCondition(Map<String, Object> params) {

        //检索到已上架的spuInfoEntity
        List<SpuInfoEntity> publish_status = spuInfoDao.selectList(new QueryWrapper<SpuInfoEntity>().eq("publish_status", 1));

        //该集合都已上架商品的ID
        List<Long> collect = publish_status.stream().map(item -> item.getId()).collect(Collectors.toList());
        QueryWrapper<SkuInfoEntity> queryWrapper = new QueryWrapper<>();
        if (publish_status != null && publish_status.size() > 0){
            queryWrapper.in("spu_id", collect);
        }else {
            //如果没有上架的商品，就构建SearchResult返回就好了
            SearchResult searchResult = new SearchResult();
            searchResult.setPageNum(1);
            searchResult.setPageNum(0);
            searchResult.setTotalPages(0L);
            searchResult.setPageNavs(new ArrayList<>());
            searchResult.setCommodity(new ArrayList<>());
            searchResult.setKeyword(params.get("keyword")==null?"":params.get("keyword").toString());
            return searchResult;

        }

        //检索条件keyword
        String keyword = (String) params.get("keyword");
        if (StringUtils.isNotBlank(keyword)){
            queryWrapper.and(w->{
                 w.eq("sku_id", keyword).or().eq("sku_name", keyword);
            });
        }

        //检索条件分类
        String catalog3Id = (String) params.get("catalog3Id");
        if (StringUtils.isNotBlank(catalog3Id) && !"0".equalsIgnoreCase(catalog3Id)){
            queryWrapper.eq("catalog_id", catalog3Id);
        }

        //检索条件 品牌id
        String brandId = (String) params.get("brandId");
        if (StringUtils.isNotBlank(brandId)&&!"0".equalsIgnoreCase(brandId)){
            queryWrapper.eq("brand_id", brandId);
        }

        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                queryWrapper
        );
        PageUtils pageUtils = new PageUtils(page);

        SearchResult searchResult = new SearchResult();
        searchResult.setCommodity((List<SkuInfoEntity>) pageUtils.getList());
        searchResult.setPageNum(pageUtils.getCurrPage());
        searchResult.setTotal((long) pageUtils.getTotalCount());
        searchResult.setKeyword(params.get("keyword")==null?"":params.get("keyword").toString());
        List<Integer> pageNavs = new ArrayList<>();
        for (int i = 1; i < pageUtils.getTotalCount(); i++) {
            pageNavs.add(i);
        }
        searchResult.setPageNavs(pageNavs);

        return searchResult;
    }

}